package com.vcredit.creditcat.web.api.product;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.utils.Base64Utils;
import com.vcredit.creditcat.utils.GZIPUtils;
import com.vcredit.creditcat.utils.RSAUtils;
import com.vcredit.creditcat.vo.CommonProductCreditCallbackParam;
import com.vcredit.creditcat.vo.payday.CommonRequest;
import com.vcredit.creditcat.vo.payday.CommonResponse;
import com.vcredit.creditcat.vo.payday.ParamRequest;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.BeanUtil;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 */
@Controller
@RequestMapping("api-mock-test")
public class MockProductRequestController {

	private static final Logger LOG = LoggerFactory.getLogger(MockProductRequestController.class);

	// 信用猫公钥（模拟回调加密）
	@Value("${response.xinyongmao.publicKey}")
	private String publicKey;
	// 合作方私钥（模拟回调加签）
	@Value("${response.parent.privateKey}")
	private String privateKey;

	//准入接口
	@Log
	@RequestMapping("/callback")
	@ResponseBody
	public Object callback(@RequestBody ParamRequest param)
			throws Exception {
		try {
			String ac_name = param.getAc_name();
			if(StringUtils.isEmpty(ac_name) || param.getBiz_data() == null) {
                return new CommonResponse().Failure("入参错误!");
			}

			CommonRequest request = new CommonRequest().init();

			Object bidData = param.getBiz_data();
			request.setBiz_data(bidData);


			//1、合作方使用信用猫平台的RSA公钥对业务参数进行RSA加密
			String compressBizData = JacksonUtil.Obj2Json(request.getBiz_data());
			byte[] data = compressBizData.getBytes("UTF-8");
			byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey);
            String base64encodedData =  Base64Utils.encode(encodedData);
            System.out.println("压缩前业务数据字符串：" + base64encodedData);

			request.setBiz_data(base64encodedData);


			//2、使用合作方自己的RSA私钥对所有参数进行签名
			String requsetJson = JacksonUtil.Obj2Json(request);
			byte[] requestData = requsetJson.getBytes("UTF-8");
			String sign = RSAUtils.sign(requestData, privateKey);
			request.setSign(sign);

			CommonProductCreditCallbackParam cp = new CommonProductCreditCallbackParam();
			BeanUtil.copy(request,cp);
			cp.setApp_id(200015);
			cp.setVersion("v1.0");

			System.out.println("网络传输前请求内容: \r\n" + JacksonUtil.Obj2Json(request));


			String queryResultStr = HttpRequest.doJson("http://localhost:8811/callback/precheckstatus", JacksonUtil.Obj2Json(cp), 20000);

			System.out.println(queryResultStr);
			return new CommonResponse().Failure(queryResultStr);
		} catch (Exception ex) {
			return new CommonResponse().Failure("未知错误");
		}
	}
}
